--
-- Author: shunguo.chen
-- Date: 2019-07-04 16:19:46
--
-- TeamMatchCrossView 跨服匹配view
--

local NewTeamMemberPanel = import(".NewTeamMemberPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.TeamMatchCrossScene

function ClassRef:init()
    self._ui.Button_close:setVisible(false)
    self._ui.Button_close:addClickEventListener(function()
        executeFunc(self._onCloseView, self)
    end)

    self._ui["Panel/Panel_bom/Button_readySure"]:addClickEventListener(function()
        if not self._info:isMySelfReady() then
            executeFunc(self._onOperate, "match_confirm", true)
        else
            -- display.pushToast(L("CrossServerTips17"))
        end
    end)
    self._ui["Panel/Panel_bom/Button_readyCancel"]:addClickEventListener(function()
        executeFunc(self._onOperate, "match_confirm", false)
    end)

    self._tltText = self._ui.tips

    local midPanel = self._ui.Panel_mid
    self._memberPanel = NewTeamMemberPanel.create(midPanel:getChildByName("player"))
    self._memberPanel:registerOnOperate(function(op, ...)
            executeFunc(self._onOperate, op, ...) -- 交由controller处理
        end)

    self._timeText = midPanel:findChild("Panel_bom/time_num")
    self._readyNumText = midPanel:findChild("Panel_bom/num")
end

-- info = TeamMatchCrossInfo
function ClassRef:refresh(info)
    self._info = info

    local teamTarget = info:getTeamTargetVo()
    self._tltText:setString(teamTarget:getFullNameWithDiff())

    local readyNum, totalNum = info:getReadyNum()
    local str = string.format(L("CrossServerTips18"), readyNum, totalNum)
    self._readyNumText:setString(str)

    self._memberPanel:refreshCrossMatch(info)

    self._timeText:stopAllActions()
    self:_refreshTime()
end

function ClassRef:_refreshTime()
    local remainTime = self._info:getRemainTime()
    if remainTime <= 0 then
        display.pushToast(L("CrossServerTips19"))
        executeFunc(self._onCloseView, self)
        return
    end
    self._timeText:setString(remainTime)
    self._timeText:performWithDelay(handler(self, self._refreshTime), 1)
end

return ClassRef
